<?php


namespace app\sys\model;


use Kqtec\IpLocationZh\Ip;
use think\facade\Cache;
use think\facade\Request;
use think\Model;

class UserActionLog extends Model
{
    protected $autoWriteTimestamp = true;

    public static function write($response_status, $response_msg, $remark = "", $request_data = "")
    {
        $start = Request::time(true) * 1000;
        $ip = Request::ip();
        $address = implode("/", Ip::find($ip)["location"]);
        $user = Request::param("_user");
        $user_no = "";
        $user_name = "";
        if ($user != null) {
            $user_no = $user["no"];
            $user_name = $user["name"];
        }

        $ms = msectime() - intval($start);
        $info = [
            "user_no" => $user_no,
            "user_name" => $user_name,
            "url" => Request::baseUrl(),
            "ip" => $ip,
            "address" => $address,
            "method" => Request::method(),
            "ms" => $ms,
            "response_status" => $response_status,
            "response_msg" => $response_msg,
            "remark" => $remark,
            "request_body" => Request::except("_user,_uuid"),
            "uuid" => Request::param("_uuid"),
            "http_status" => 200,
            "action" => Request::action(),
            "user_agent" => Request::header("user-agent"),
            "request_data" => $request_data,
            "request_head" => Request::header("token"),
        ];
        UserActionLog::create($info);
    }
}